<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" v-if="dialogVisible" :close-on-click-modal="false" width="30%"
    :before-close="handleClose">
    <div>
      <el-form :model="ruleForm" :rules="rules" size="mini" ref="ruleForm" label-width="110px" class="demo-ruleForm">
        <el-form-item label="系统名称" prop="systemId">
          <!-- <el-input v-model="ruleForm.sysName"></el-input> -->
          <el-select v-model="ruleForm.systemId" placeholder="请选择" style="width: 100%" @change="selectSysChanged">
            <el-option v-for="item in systemOption" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="环境名称" prop="environmentId">
          <el-select v-model="ruleForm.environmentId" placeholder="请选择" style="width: 100%" @change="selectEnvChanged">
            <el-option v-for="item in envOption" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="服务名称" prop="serviceId">
          <el-select v-model="ruleForm.serviceId" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in serviceOption" :key="item.reId" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
          <!-- <el-input v-model="ruleForm.serviceName"></el-input> -->
        </el-form-item>

        <el-form-item label="IP" prop="ip">
          <el-input v-model="ruleForm.ip" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="端口" prop="port">
          <el-input v-model="ruleForm.port" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="日志名称" prop="name">
          <el-input v-model="ruleForm.name" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="日志文件路径的正则表达式" prop="fileRegularExpression">
          <el-input v-model="ruleForm.fileRegularExpression" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="关键字列表" prop="keyWordList">
          <el-input v-model="ruleForm.keyWordList" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    systemOption: {
      type: Array,
      default: () => [],
    },
    envOption: {
      type: Array,
      default: () => [],
    },
    serviceOption: {
      type: Array,
      default: () => [],
    },
  },
  components: {},
  data() {
    return {
      title: "新增日志",
      dialogVisible: false,
      ruleForm: {
        id: "",
        serviceId: "",
        systemId: "",
        environmentId: "",
        ip: "",
        port : "",
        username: "",
        password: "",
        name: "",
        fileRegularExpression: "",
        keyWordList: "",
      },
      rules: {
        systemId: [
          { required: true, message: "请选择系统名称", trigger: "change" },
        ],
        environmentId: [
          { required: true, message: "请选择环境名称", trigger: "change" },
        ],
        serviceId: [
          { required: true, message: "请选择服务名称", trigger: "change" },
        ],
        ip: [
          { required: true, message: "请输入IP", trigger: "blur" },
        ],
        port: [
          { required: true, message: "请输入端口", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
        name: [{ required: true, message: "请输入日志名称", trigger: "blur" }],
        fileRegularExpression: [{ required: true, message: "请输日志文件路径的正则表达式", trigger: "blur" }],
        keyWordList: [{ required: true, message: "关键字列表，用英文逗号分割", trigger: "blur" }],
      },
    };
  },
  methods: {
    selectSysChanged() {
      this.$emit("selectSysChanged", this.ruleForm.systemId);
      this.ruleForm.environmentId = "";
    },
    selectEnvChanged() {
      this.$emit(
        "selectEnvChanged",
        this.ruleForm.environmentId,
        this.ruleForm.systemId
      );
      this.ruleForm.serviceId = "";
    },
    open(data) {
      this.title = "新增日志";
      if (data) {
        this.title = "编辑日志";
        this.ruleForm = Object.assign(this.ruleForm, data);
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.resetForm("ruleForm");
    },
    close() {
      this.resetForm("ruleForm");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("save", this.ruleForm);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.ruleForm = {
        id: "",
        serviceId: "",
        systemId: "",
        environmentId: "",
        name: "",
        fileRegularExpression: "",
        keyWordList: "",
      };
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
      this.$emit("updateType");
    },
  },
  created() { },
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>
